/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.tag.page;


import java.util.Locale;
import java.util.ResourceBundle;

import com.legendshop.dao.support.DefaultPagerProvider;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.dao.support.ToolBarProvider;


/**
 * 默认工具条
 */
public class SimplePageProviderImpl extends DefaultPagerProvider implements ToolBarProvider{
	
	public static final String LOCALE_FILE = "i18n/ApplicationResources";
	
	private String actionUrl;
	
	public SimplePageProviderImpl(long total, String curPage, int pageSize, String actionUrl) {
		super(total, curPage, pageSize);
		this.actionUrl =actionUrl;
	}
	
	public SimplePageProviderImpl(long total, int curPage, int pageSize, String actionUrl) {
		super(total, curPage, pageSize);
		this.actionUrl =actionUrl;
	}
	
	public SimplePageProviderImpl(PageSupport ps, String actionUrl) {
		super(ps.getTotal(), ps.getCurPageNO(), ps.getPageSize());
		this.actionUrl =actionUrl;
	}
	
	public SimplePageProviderImpl(PageSupport ps) {
		super(ps.getTotal(), ps.getCurPageNO(), ps.getPageSize());
		this.actionUrl = "javascript:pager";//默认的动作
	}

	@Override
	public String getToolBar() {
		if( hasMutilPage()){//有分页条
			return this.getBar(null, actionUrl);
		}else{
			return null;
		}
	}
	
	/**
	 * 前台工具条
	 */
	
	public String getBar(Locale locale, String url) {
		if(locale == null){
			locale = Locale.CHINA;
		}
		StringBuilder str = new StringBuilder();
		//str.append("<span class='pagerTotal'>共").append(total).append("个记录</span>");
		str.append("<span class='p-num'>");

		//1. 显示上一页
		if (isFirst()) {//位于第一页
			// 第一页不用加超链接
			str.append("<a class='pn-prev disabled' >").append("<em>上一页</em>").append("</a>");
		} else {
			str.append("<a class='pn-prev' href='").append(url).append("(").append(previous()).append(")'>")
					.append("&nbsp;").append(getString(locale, "pager.previous", "Previous")).append("&nbsp;").append("</a>");
		}
		
		//2. 显示首页
		appendItem( str,  1, url);
		
		//3. 显示当前页前后2页
		int d1 = curPageNO - 1; //当前页与第一页的间距
		int d2 = pageCount - curPageNO; //当前页与最后一页的间距
		int start = Math.max(curPageNO - 2, 2);
		int end =  Math.min(curPageNO + 2, pageCount - 1) ;
		//3.1 加入点号
		if(d1 > 3){
			appendEllipsisItem(str);
		}
		for (int i = start; i <= end; i++) {
			appendItem(str, i,url);
		}
		//3.2 加入点号
		if(d2 > 3){
			appendEllipsisItem(str);
		}
		//4. 显示尾页
		appendItem( str,  pageCount, url);
		//5. 显示下一页
		if (isLast() || (total== 0)) {
			//str.append("&nbsp;&nbsp;");
			str.append("<a class='pn-next disabled' ><em>下一页</em></a>");
		} else {
			/*str.append("<a class='pagerDirectionRight' href='").append(url).append("(").append(next()).append(")'>")
					.append("&nbsp;").append(getString(locale, "pager.next", "Next")).append("&nbsp;").append("</a>");*/
			str.append("<a class='pn-next' href='").append(url).append("(").append(next()).append(")'>").append("<em>下一页</em></a>");
		}

		return str.toString();
	}
	
	/**
	 * 加入页码
	 * @param str
	 * @param i
	 * @param url
	 */
	private void appendItem(StringBuilder str, int i,String url){
		if (i == getCurPageNO()) {
			//str.append("<span class='pagerSelected'>").append(i).append("</span>");
			str.append("<a href='javascript:void(0);' class='curr'>").append(i).append("</a>");
		} else {
			str.append("<a href='").append(url).append("(").append(i).append(")'>").append(i).append("</a>");
		}
	}
	
	/**
	 * 加入省略号
	 * @param str
	 */
	private void appendEllipsisItem(StringBuilder str){
		//str.append("<span class='pagerUnselected' style='font-size: 7px'>\u2026</span>");
		str.append("<a href='javascript:void(0);' ><em>...</em></a>");
	}
	
	/**
	 * 获得I18n的国际化语言
	 */
	protected String getString(Locale locale, String key, String defaultValue) {
		String value;
		try {
			if (locale != null) {
				value = ResourceBundle.getBundle(LOCALE_FILE, locale).getString(key);
			} else {
				value = ResourceBundle.getBundle(LOCALE_FILE).getString(key);
			}
			if (value == null) {
				value = defaultValue;
			}
		} catch (Exception e) {
			System.out.println(e.getLocalizedMessage());
			value = defaultValue;
		}

		return value;
	}

}
